<?php
/**
 * Case insensitive version of array_key_exists. 
 * Returns the matching key on success, else false. 
 * 
 * @param string $key 
 * @param array $search 
 * @return string|false 
 */
function array_key_exists_nc($key, $search) {
    if (array_key_exists($key, $search)) {
        return $key;
    }
    if (!(is_string($key) && is_array($search) && count($search))) {
        return false;
    }
    $key = strtolower($key);
    foreach ($search as $k => $v) {
        if (strtolower($k) == $key) {
            return $k;
        }
    }
    return false;
}

function arrayfile_to_array($filepath) {
// This function reads the file at $filepath and returns an
// array. It is assumed that the file being read is a
// serialized array (created using array_to_arrayfile)
	  $fsize = @filesize($filepath);
	  if ($fsize > 0) {
	    $fp = fopen($filepath, "r");
	    $encoded = fread($fp, $fsize);
	    fclose($fp);
	    return unserialize($encoded);
	  }
	  else
	      echo "$filepath does not exist!";
}

function array_to_arrayfile($theArray, $filepath) {
// This function serializes an array in $theArray and saves it
// in the file at $filepath. The file may then be converted
// back into an array using the arrayfile_to_array() function)

	  if($fp = fopen($filepath, "w+")) {
	    $encoded = serialize($theArray);
	    fwrite($fp, $encoded);
	    fclose($fp);
	  }
	  else
	    echo "Unable to write array to $filepath";
}
?>